package com.kk.springmvc.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kk.springmvc.demo.bean.PersonDTO;
import com.kk.springmvc.demo.common.utils.FastJsonUtils;
import com.kk.springmvc.demo.service.IPersonService;

/**
* PersonController.java
* 类说明 TODO
*
* @author chengkai
* @date  2017年6月9日 下午2:51:36
*/
@Controller
public class PersonController extends BaseController {

    @Autowired
    private IPersonService personService;

    @RequestMapping(value = "/person/create", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(@RequestBody String requestJson, HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<>();
        PersonDTO personDTO = FastJsonUtils.toBean(requestJson, PersonDTO.class);
        personService.create(personDTO);
        createSuccessResult(resultMap);
        return resultMap;
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/person/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(@RequestBody String requestJson, HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, String> param = (Map<String, String>) FastJsonUtils.stringToMap(requestJson);
        List<PersonDTO> personDTOList = personService.list(Integer.valueOf(String.valueOf(param.get("pageNum"))));
        createSuccessResult(resultMap);
        resultMap.put("data", personDTOList);
        return resultMap;
    }

}
